﻿package mortal.common
{
    import com.gengine.core.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import flash.display.*;
    import flash.geom.*;
    import mortal.component.gconst.*;

    public class DisplayUtil extends Object
    {

        public function DisplayUtil()
        {
            return;
        }// end function

        public static function removeAllChild(param1:DisplayObjectContainer) : void
        {
            while (param1.numChildren > 0)
            {
                
                param1.removeChildAt(0);
            }
            return;
        }// end function

        public static function getStringPixes(param1:String, param2:int = 12) : int
        {
            var _loc_3:int = 0;
            if (param1 == null || param1 == "")
            {
                return _loc_3;
            }
            var _loc_4:int = 0;
            while (_loc_4 < param1.length)
            {
                
                if (param1.charCodeAt(_loc_4) < 255)
                {
                    _loc_3 = _loc_3 + param2 / 2;
                }
                else
                {
                    _loc_3 = _loc_3 + param2;
                }
                _loc_4++;
            }
            return _loc_3;
        }// end function

        public static function removeMe(param1:DisplayObject) : void
        {
            if (param1 == null)
            {
                return;
            }
            if (param1.parent != null)
            {
                param1.parent.removeChild(param1);
            }
            return;
        }// end function

        public static function getBitMapData(param1:DisplayObject) : BitmapData
        {
            if (param1 == null || param1.width < 1 || param1.height < 1)
            {
                return null;
            }
            var _loc_2:* = new BitmapData(param1.width, param1.height);
            _loc_2.draw(param1);
            return _loc_2;
        }// end function

        public static function isBitmapDataUsable(param1:BitmapData) : Boolean
        {
            var bitmapData:* = param1;
            if (bitmapData == null)
            {
                return false;
            }
            try
            {
                bitmapData.getPixel32(1, 1);
            }
            catch (err:Error)
            {
                return false;
            }
            return true;
        }// end function

        public static function drawFadingEllipse(param1:Sprite, param2:uint, param3:int, param4:int, param5:Number = 0.13, param6:Number = 0.8, param7:int = 3) : void
        {
            var _loc_10:Number = NaN;
            if (param1 == null)
            {
                return;
            }
            param1.graphics.clear();
            var _loc_8:* = param4 / param3;
            var _loc_9:int = 1;
            while (_loc_9 <= param3)
            {
                
                _loc_10 = param6 - _loc_9 / param3 * (param6 - param5);
                param1.graphics.lineStyle((param7 + 1), param2, _loc_10);
                param1.graphics.drawEllipse(-_loc_9, (-_loc_9) * _loc_8, _loc_9 * 2, _loc_9 * 2 * _loc_8);
                _loc_9 = _loc_9 + param7;
            }
            param1.cacheAsBitmap = true;
            return;
        }// end function

        public static function drawFullScreenMask(param1:GSprite, param2:uint = 0, param3:Number = 0.6) : void
        {
            param1.graphics.clear();
            param1.graphics.beginFill(param2, param3);
            param1.graphics.drawRect(0, 0, Global.stage.stageWidth, Global.stage.stageHeight);
            param1.graphics.endFill();
            param1.cacheAsBitmap = true;
            return;
        }// end function

        public static function drawFullScreenMaskByStageXY(param1:GSprite, param2:uint = 0, param3:Number = 0.6) : void
        {
            var _loc_4:* = param1.localToGlobal(new Point(0, 0));
            param1.graphics.clear();
            param1.graphics.beginFill(param2, param3);
            param1.graphics.drawRect(-_loc_4.x, -_loc_4.y, Global.stage.stageWidth, Global.stage.stageHeight);
            param1.graphics.endFill();
            param1.cacheAsBitmap = true;
            return;
        }// end function

        public static function drawHollowMaskByRect(param1:Sprite, param2:Rectangle, param3:uint = 0, param4:Number = 0.6, param5:int = -1, param6:int = -1) : void
        {
            if (param5 <= -1)
            {
                param5 = Global.stage.stageWidth;
            }
            if (param6 <= 0)
            {
                param6 = Global.stage.stageHeight;
            }
            param1.graphics.clear();
            param1.graphics.beginFill(param3, param4);
            param1.graphics.drawRect(0, 0, param5, param2.y);
            param1.graphics.drawRect(0, param2.y, param2.x, param2.height);
            param1.graphics.drawRect(param2.x + param2.width, param2.y, param5 - param2.x - param2.width, param2.height);
            param1.graphics.drawRect(0, param2.y + param2.height, param5, param6 - param2.y - param2.height);
            param1.graphics.endFill();
            param1.cacheAsBitmap = true;
            return;
        }// end function

        public static function getChildByPropety(param1:DisplayObjectContainer, param2:String, param3) : DisplayObject
        {
            var _loc_5:DisplayObject = null;
            var _loc_7:DisplayObject = null;
            if (param1 == null || param1.numChildren == 0)
            {
                return null;
            }
            var _loc_4:* = param1.numChildren;
            var _loc_6:int = 0;
            while (_loc_6 < _loc_4)
            {
                
                _loc_7 = param1.getChildAt(_loc_6);
                if (_loc_7 != null && _loc_7.hasOwnProperty(param2) && _loc_7[param2] == param3)
                {
                    return _loc_7;
                }
                if (_loc_7 is DisplayObjectContainer)
                {
                    _loc_5 = getChildByPropety(_loc_7 as DisplayObjectContainer, param2, param3);
                    if (_loc_5 != null)
                    {
                        return _loc_5;
                    }
                }
                _loc_6++;
            }
            return null;
        }// end function

        public static function getRelativePlace(param1:DisplayObject, param2:DisplayObject) : Point
        {
            var _loc_3:* = new Point(0, 0);
            if (param1 == null || param2 == null)
            {
                return _loc_3;
            }
            var _loc_4:* = param1.localToGlobal(_loc_3);
            var _loc_5:* = param2.localToGlobal(_loc_3);
            _loc_3.x = _loc_5.x - _loc_4.x;
            _loc_3.y = _loc_5.y - _loc_4.y;
            return _loc_3;
        }// end function

        public static function removeAndDisposeAllGUIComponent(param1:DisplayObjectContainer, param2:Boolean = true, param3:Boolean = true) : void
        {
            var _loc_6:DisplayObject = null;
            if (param1 == null)
            {
                return;
            }
            var _loc_4:* = param1.numChildren;
            var _loc_5:* = _loc_4 - 1;
            while (_loc_5 >= 0)
            {
                
                _loc_6 = param1.getChildAt(_loc_5);
                if (_loc_6 == null)
                {
                }
                else
                {
                    param1.removeChild(_loc_6);
                    if (_loc_6 is GUIComponent)
                    {
                        GUIComponent(_loc_6).dispose(param2);
                    }
                    else if (param3 && _loc_6 is DisplayObjectContainer)
                    {
                        removeAndDisposeAllGUIComponent(_loc_6 as DisplayObjectContainer, param2, param3);
                    }
                }
                _loc_5 = _loc_5 - 1;
            }
            return;
        }// end function

        public static function setEnabled(param1:InteractiveObject, param2:Boolean, param3:Boolean = true) : void
        {
            if (param1 == null)
            {
                return;
            }
            if (param1["enabled"] != null)
            {
                param1["enabled"] = param2;
            }
            else if (param2)
            {
                param1.filters = [];
            }
            else
            {
                param1.filters = [FilterConst.colorFilter];
            }
            if (param3)
            {
                param1.mouseEnabled = param2;
            }
            return;
        }// end function

        public static function calculateFixedScale(param1:int, param2:int, param3:int, param4:int) : Number
        {
            var _loc_7:Number = NaN;
            var _loc_5:* = param1 / param2;
            var _loc_6:* = param3 / param4;
            if (_loc_5 > _loc_6)
            {
                _loc_7 = param2 / param4;
            }
            else
            {
                _loc_7 = param1 / param3;
            }
            return _loc_7 > 1 ? (1) : (_loc_7);
        }// end function

        public static function delNotUse(param1:Array, param2:int, param3:Boolean = true) : void
        {
            var _loc_6:DisplayObject = null;
            if (param1 == null || param1.length <= param2)
            {
                return;
            }
            var _loc_4:* = param2;
            var _loc_5:* = param2;
            while (_loc_5 < param1.length)
            {
                
                _loc_6 = param1[_loc_5];
                if (_loc_6 is IDispose)
                {
                    (_loc_6 as IDispose).dispose(param3);
                }
                else
                {
                    removeMe(_loc_6);
                }
                _loc_5++;
            }
            if (_loc_4 < _loc_5 && param1.length >= _loc_4)
            {
                param1.splice(_loc_4);
            }
            return;
        }// end function

    }
}
